<template>
	<view class="form">
		<view class="form__title">
			<image class="form__title__background"
				:src="require('@/uni_modules/zm-login-account/pages/login/static/login_icon_title_line_back.png')" />
			<view class="form__title__content">登录</view>
		</view>
		<view class="form__subtitle"> 欢迎来到私人定制小程序 </view>
		<view class="form__fieldset">
			<view class="form__field">
				<input class="form__field__control" placeholder-class="form__field__placeholder"
					v-model="infoData.mobile" type="number" :maxlength="11" placeholder="手机号" @blur="handlePhoneBlur" />
				<image class="form__field__icon" v-if="infoData.mobile.length > 0"
					:src="require('@/uni_modules/zm-login-account/pages/login/static/login_icon_clear.png')"
					@click="infoData.mobile = ''" />
			</view>
			<view class="form__field">
				<input class="form__field__control" placeholder-class="form__field__placeholder"
					v-model="infoData.psdStr" type="text" placeholder="密码" :password="!showPsd" />
				<image class="form__field__icon" :src="passwordInputIcon" @click="showPsd = !showPsd" />
			</view>
			<view class="form__flex">
				<view class="form__flex__registerV" @click="clickRegister">
					注册
				</view>
				<view class="form__flex__forgetV" @click="clickForget">
					忘记密码
				</view>
			</view>
		</view>
		<view class="form__loginBtn" @click="handleLogin">{{logining == true ?"登录中...":"登录"}}</view>
	</view>
</template>

<script>
	export default {
		name: "account-password-login-view",
		components: {},
		props: {
			logining: {
				default: false,
				type: Boolean
			}
		},

		data: function() {
			return {
				infoData: {
					mobile: '13588888888',
					psdStr: '123456',
				},
				showPsd: false,
			}
		},

		computed: {
			passwordInputIcon() {
				return this.showPsd ?
					require('@/uni_modules/zm-login-account/pages/login/static/login_icon_pwd_show.png') :
					require('@/uni_modules/zm-login-account/pages/login/static/login_icon_pwd_hidden.png');
			},
		},

		methods: {
			validatePhone() {
				if (!/^\d{11}$/.test(this.infoData.mobile)) {
					wx.showToast({
						icon: 'none',
						title: '请输入正确的手机号',
					});
					return false;
				}
				return true;
			},
			validatePassword() {
				if (!this.infoData.psdStr.trim()) {
					wx.showToast({
						icon: 'none',
						title: '请输入正确的密码',
					});
					return false;
				}
				return true;
			},
			validate() {
				return this.validatePhone() && this.validatePassword();
			},
			handleLogin(event) {
				if (this.validate()) {
					this.$emit('login', this.infoData);
				}
			},
			handlePhoneBlur() {
				wx.nextTick(() => {
					if (this.infoData.mobile) {
						this.validatePhone();
					}
				});
			},
			
			clickRegister() {
				this.$emit("register");
			},
			clickForget() {
				this.$emit("forget");
			},
		},
	}
</script>

<style lang="scss" scoped>
	.form {
		margin: 0rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;

		&__title {
			position: relative;
			width: 100%;
			text-align: left;
			margin-top: 0rpx;
			margin-bottom: 24rpx;
			line-height: 78rpx;
			font-size: 56rpx;
			color: #1c1c1c;

			&__background {
				position: absolute;
				bottom: 0;
				left: 0;
				width: 113rpx;
				height: 36rpx;
			}
		}

		&__subtitle {
			width: 100%;
			text-align: left;
			margin-bottom: 92rpx;
			line-height: 45rpx;
			font-size: 32rpx;
			color: #999999;
		}

		&__fieldset {
			width: 100%;
		}

		&__field {
			position: relative;
			border-bottom: 1px solid #eeeeee;

			&__control {
				box-sizing: border-box;
				height: 90rpx;
				padding-right: 48rpx;
				line-height: 90rpx;
				font-size: 36rpx;
				color: #333333;
			}

			&__icon {
				position: absolute;
				top: 29rpx;
				right: 0;
				width: 32rpx;
				height: 32rpx;
			}

			&__placeholder {
				font-size: 36rpx;
				color: #bbbbbd;
			}
		}

		&__flex {
			display: flex;
			flex-direction: row;
			align-items: center;
			justify-content: space-between;
			margin-top: 10rpx;
			
			font-size: 28rpx;
			color: #666666;
			text-align: center;
			
			&__registerV {
				padding-top: 10rpx;
				border-bottom: 2rpx solid #999999;
			}

			&__forgetV {
				padding-top: 10rpx;
				border-bottom: 2rpx solid #999999;
			}
		}

		&__loginBtn {
			margin-top: 25vh;
			width: 100%;
			height: 88rpx;
			line-height: 88rpx;
			border-radius: 44rpx;
			box-shadow: 0 2px 10px 0 rgba(255, 176, 0, 0.5);
			background: #ffd100;
			font-size: 36rpx;
			color: #1c1c1c;
			text-align: center;
		}
	}
</style>
